package com.jsb.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jsb.common.utils.IdGen;
import com.jsb.common.utils.JsonUtil;
import com.jsb.common.utils.StringUtils;
import com.jsb.model.Category;
import com.jsb.model.Drama;
import com.jsb.model.DramaExample;
import com.jsb.model.Teacher;
import com.jsb.service.CategoryService;
import com.jsb.service.DramaService;
import com.jsb.service.TeacherService;

@Controller
public class DramaController{
	
	public static final Logger log = Logger.getLogger(DramaController.class);
	
	private int defaultPageSize=10;
	
	private String redirect = "redirect:/drama/list.do";
	
	@Resource
	private DramaService dramaService;
	
	@Resource
	private CategoryService categoryService;
	@Resource
	private TeacherService teacherService;
	
	@RequestMapping("/drama/list")
	public String list(
			Model model,
			@RequestParam(value = "pageNum", required = false) Integer pageNum,
			@RequestParam(value = "pageSize", required = false) Integer pageSize) {
		int maxPageNum = 0;
		if (pageSize == null) {
			pageSize = defaultPageSize;
		}
		DramaExample dramaExample = new DramaExample();
		int totalCount = dramaService.countDrama(dramaExample);
		if(totalCount%pageSize == 0){
			maxPageNum = totalCount/pageSize;
		}else{
			maxPageNum = totalCount/pageSize+1;
		}
		if (pageNum == null || pageNum < 1) {
			pageNum = 1;
		}else {
			if(maxPageNum > 0 && pageNum > maxPageNum) {
				pageNum = maxPageNum;
			}
		}
		dramaExample.setPageIndex((pageNum - 1) * pageSize);
		dramaExample.setPageSize(pageSize);
		List<Drama> dramas = dramaService.getPageDrama(dramaExample);
		for (Drama drama : dramas) {
			String categoryId = drama.getCategoryId();
			String lable = "";
			if(categoryId.contains(",")){
				String[] split = categoryId.split(",");
				for (String str : split) {
					Category categoryByUuid = categoryService.getCategoryByUuid(str);
					String name = categoryByUuid.getName();
					lable+=name+",";
				}
			}else{
				Category categoryByUuid = categoryService.getCategoryByUuid(categoryId);
				lable = categoryByUuid.getName();
			}
			drama.setCategoryId(lable);
			String teacherId = drama.getTeacherId();
			Teacher teacher = teacherService.getTeacherById(teacherId);
			if(null != teacher){
				drama.setTeacherId(teacher.getName());
			}
		}
		model.addAttribute("totalCount", totalCount);
		model.addAttribute("pageNum", pageNum);
		model.addAttribute("pageSize", pageSize);
		if(totalCount%pageSize == 0){
			model.addAttribute("maxPageNum", totalCount/pageSize);
		}else{
			model.addAttribute("maxPageNum", totalCount/pageSize+1);
		}
		model.addAttribute("dramas", dramas);
		return "/Drama/list";
	}
	
	@RequestMapping("/drama/toAdd")
	public String toAdd(Model model) {
		List<Teacher> teachers = teacherService.selectByExample(null);
		List<Category> categoryList = categoryService.getCategoryList("0");
		model.addAttribute("categoryList", categoryList);
		model.addAttribute("teachers", teachers);
		return "/Drama/add";
	}
	@RequestMapping("/drama/getCategoryList")
	@ResponseBody
	public Object getCategoryList(String id){
		List<Category> categoryList = categoryService.getCategoryList(id);
		return categoryList;
	}
	
	@RequestMapping("/drama/add")
	public String add(Model model, @ModelAttribute("drama") Drama drama,String categoryId) {
		Map<String,String> map = new HashMap<String, String>();
		String label = "";
		if(StringUtils.isNotEmpty(categoryId)){
			String[] split = categoryId.split(",");
			for (String string : split) {
				if(StringUtils.isNotEmpty(string)){
					label+=string+",";
				}
			}
		}
		label = label.trim().substring(0, label.length()-1);
		if(StringUtils.isEmpty(label)){
			map.put("msg", "未选择标签");
			String json = JsonUtil.toJson(map);
			model.addAttribute("json", json);
			return "json";
		}
		drama.setUuid(IdGen.uuid());
		drama.setCategoryId(label);
		drama.setCreateDate(new Date());
		dramaService.addDrama(drama);
		return redirect;
	}

	@RequestMapping("/drama/del")
	public String del(Model model,
			@RequestParam(value = "uuid", required = true) String uuid) {
		dramaService.delDramaByUuid(uuid);
		return redirect;
	}

	@RequestMapping("/drama/toEdit")
	public String toEdit(Model model,
			@RequestParam(value = "uuid", required = true) String uuid) {
		Drama drama = dramaService.getDramaByUuid(uuid);
		model.addAttribute("drama", drama);
		List<Category> categoryList = categoryService.getCategoryList("0");
		model.addAttribute("categoryList", categoryList);
		return "/Drama/edit";
	}

	@RequestMapping("/drama/edit")
	public String edit(Model model, @ModelAttribute("drama") Drama drama) {
		dramaService.updateDrama(drama);
		return redirect;
	}

	@RequestMapping("/drama/view")
	public String view(Model model,
			@RequestParam(value = "uuid", required = true) String uuid) {
		Drama drama = dramaService.getDramaByUuid(uuid);
		String categoryId = drama.getCategoryId();
		String lable = "";
		if(categoryId.contains(",")){
			String[] split = categoryId.split(",");
			for (String str : split) {
				Category categoryByUuid = categoryService.getCategoryByUuid(str);
				String name = categoryByUuid.getName();
				lable+=name+",";
			}
		}else{
			Category categoryByUuid = categoryService.getCategoryByUuid(categoryId);
			lable = categoryByUuid.getName();
		}
		drama.setCategoryId(lable);
		model.addAttribute("drama", drama);
		return "/Drama/view";
	}
}
